require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#commoditySearch",
    data:{
        name:"",
        type:"",
        isScreen:false,
        loadFlag:false,
        list:{
            loginUid:userInfo.id,
            // loginUid:1,
            pageNumber:1
        },
        title:"",
        num:"",
        // isScreen:"",
        noticeList:{},//列表
        noticeList1:[],
        noticeList2:[]
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        // this.myList();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取列表
        myList:function(){
            var that=this;
            var url;
            var data=that.list;
            if(that.type==1){
                data.title=that.title;
                url="/happyi/myAllProduct";
            }else if(that.type==2){
                data.title=that.title;
                url="/happyi/sellerorderList";
            }else if(that.type==3){
                data.name=that.title;
                data.type=0;
                url="/purchase/userOrderList";
            }
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.noticeList=b.json.data;
                    that.noticeList1=that.noticeList.list;
                    if(that.noticeList.pageNumber>1){
                        if(that.noticeList.list.length>0) {
                            that.noticeList2 = that.noticeList2.concat(b.json.data.list);
                        }
                    }else{
                        that.noticeList2=that.noticeList1;
                    }
                    console.log(that.noticeList2);
                    vm.loadFlag=false;
                }else{
                    that.noticeList={};
                    that.noticeList1=[];
                    that.noticeList2=[];
                    vm.$refs.prompt.toast("获取列表失败！");
                }
            });

        },
        //是否上下架
        upperLowerProduct:function(item){
            var that=this;
            var data={
                loginUid:that.list.loginUid,
                productId:item.productId
            };
            if(item.isupper==1) {
                data.state = 0;
            }else{
                data.state=1;
            }
            this.ajax({url:"/happyi/upperLowerProduct",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.myList();
                }else{
                    vm.$refs.prompt.toast("操作失败！");
                }
            });

        },
        compareDate:function(item){
            var that=this;
            var second=getCompareDate(item.currentTime,item.limitTime,"second");
            return second;
        },
        //时分秒倒计时
        leftTimer:function(item,index){
            var that=this;
            that.$nextTick(function(){
                var second=getCompareDate(item.currentTime,item.limitTime,"second");
                addTimer("stateData"+index,second);
            });
        },
        getNext1:function(num,productId){
            if(num==1){
                location.href="/happyi/commoditySearch?type=1";
            }else if(num==2){
                location.href="/addGoods?type=1&productId="+productId;
            }else if(num==3){
                location.href="/purchase/myOrderInfo?orderNumber="+productId+"&uid="+this.list.loginUid;
            }
        }
    }
    ,watch:{
        name:function(){
            var that=this;
            that.title=that.name;
            console.log(that.title);
            if(that.title){
                that.myList();
            }else{
                that.noticeList={};
                that.noticeList1=[];
                that.noticeList2=[];
            }

        }
    }
});
var addTimer = function () {
    var list = [],
        interval;

    return function (id, time) {
        if (!interval)
            interval = setInterval(go, 1000);
        list.push({ ele: document.getElementById(id), time: time });
    };
    function go() {
        for (var i = 0; i < list.length; i++) {
            list[i].ele.innerHTML= getTimerString(list[i].time ? list[i].time -= 1 : 0);
            if (!list[i].time)
                list.splice(i--, 1);
        }
    }
    function getTimerString(time) {
        h = Math.floor(time/ 3600);
        m = Math.floor((time% 3600) / 60);
        s = Math.floor((time% 3600) % 60);
        if (time>0)
            return checkTime(h) + ":" + checkTime(m) + ":" + checkTime(s);
        else return "00:00:00";
    }
    function checkTime(i) {
        if (i<10) {
            i = "0" +i;
        }
        return i;
    }
} ();
//比较日前大小
function getCompareDate(startTime, endTime, diffType) {
    startTime = JSON.stringify(startTime).split("-").join("/");//replace(/-/g, '/')
    endTime = JSON.stringify(endTime).split("-").join("/");
    //将计算间隔类性字符转换为小写
    diffType = diffType.toLowerCase();
    var sTime =new Date(JSON.parse(startTime)); //开始时间
    var eTime =new Date(JSON.parse(endTime)); //结束时间
    //作为除数的数字
    var timeType =1;
    switch (diffType) {
        case"second":
            timeType =1000;
            break;
        case"minute":
            timeType =1000*60;
            break;
        case"hour":
            timeType =1000*3600;
            break;
        case"day":
            timeType =1000*3600*24;
            break;
        default:
            break;
    }
    return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.noticeList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.list.pageNumber++;
            vm.myList();
        }else{
            // console.log("11111");
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};